package com.enjoy.book.action;

import com.enjoy.book.bean.Member;
import com.enjoy.book.bean.Record;
import com.enjoy.book.bean.User;
import com.enjoy.book.biz.MemberBiz;
import com.enjoy.book.biz.RecordBiz;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐佳彭
 */
@WebServlet("/record.let")
public class RecordServlet extends HttpServlet {
    RecordBiz recordBiz=new RecordBiz();
    MemberBiz memberBiz=new MemberBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        /**
         * 输出流对象,帮助我们打印信息
         */
        PrintWriter out =resp.getWriter();
        String type=req.getParameter("type");
        HttpSession session=req.getSession();
        User user=(User)session.getAttribute("user");
        switch(type){
            case "add":
                long memberId =Long.parseLong(req.getParameter("mid"));

                /**
                 * 借阅的图书编号
                 */
                String ids=req.getParameter("ids");
                String []strs=ids.split("_");
                List<Long> bookIds=new ArrayList<Long>();
                for(String s:strs){
                    Long id=Long.parseLong(s);
                    bookIds.add(id);
                }
                /**
                 * 处理当前借阅的管理员
                 */
                Long userId=user.getId();
                int count=recordBiz.add(bookIds,memberId,userId);
                System.out.println(count);
                if(count>0){
                    out.println("<script>alert('图书借阅成功');location.href='main.jsp';</script>");
                }else{
                    out.println("<script>alert('图书借阅失败');location.href='main.jsp';</script>");
                }
                break;
            case "queryback":
                //1.获取会员的身份证号
                long idn = Long.parseLong(req.getParameter("idn"));
                //2.获取会员对象和所有的未还的记录
                Member member = memberBiz.getById(idn);
                List<Record> records = recordBiz.getRecordByMemberId(member.getId());
                //3.存request
                req.setAttribute("member",member);
                req.setAttribute("records",records);
                //4.转发
                req.getRequestDispatcher("return_list.jsp").forward(req,resp);
                break;
            case "back":
                //1.会员编号
                long memberId2 = Long.parseLong(req.getParameter("mid"));
                //2.获取记录编号
                String idStr = req.getParameter("ids");
                String []idStrs = idStr.split("_");
                List<Long>  recordIds = new ArrayList<Long>();
                for(String s:idStrs){
                    recordIds.add(Long.parseLong(s));
                }
                //3.获取操作的用户编号
                long userId2 = user.getId();
                //4.调用biz
                int count2 = recordBiz.modify(memberId2, recordIds,userId2);
                //5.归还
                if(count2>0){
                    out.println("<script>alert('归还成功');location.href='main.jsp';</script>");
                }else{
                    out.println("<script>alert('归还失败');location.href='main.jsp';</script>");
                }
                break;
                default:
                resp.sendError(404,"请求的地址不存在");
        }

    }
}
